<?php
namespace app\beautiful\logic;
use app\beautiful\model\CouponModel;
use app\beautiful\model\mall\FlashSaleGoodsModel;
use app\beautiful\model\mall\GoodsModel;
use app\beautiful\model\mall\PinTuanGoodsModel;
use app\beautiful\model\mall\StoreGoodsModel;
use app\beautiful\model\member\MemberCouponModel;
use app\beautiful\model\member\MemberModel;
use app\beautiful\model\project\OnceCardModel;
use app\beautiful\model\project\ProjectModel;
use app\beautiful\model\project\SnapUpModel;
use app\beautiful\model\LookModel;
class ConDataLogic{
    protected $shopId = 0;
    protected $memberId=0;
    protected $today;
    public function __construct($shop_id,$member_id)
    {
        $this->shopId=$shop_id;
        $this->memberId=$member_id;
        $this->today=date('Y-m-d', time());
    }

    public function getData(array $params)
    {
        $datas=[];
        foreach ($params as $param){
            switch ($param['key']){
                case 'coupon':
                    $datas[$param['key']]=$this->getCouponData($param['num']);
                    break;
                case 'miaosha' :// 秒杀
                    $datas[$param['key']]=$this->getFlashSaleData($param['num']);
                    break;
                case 'booking' ://预约
                    $datas[$param['key']]=$this->getPreSaleData($param['num']);
                    break;
                case 'pintuan' : //拼团
                    $datas[$param['key']]=$this->getPinTuanData($param['num']);
                    break;
                case 'store' :
                    $datas[$param['key']]=$this->getStoreData($param['num']);
                    break;
                case 'product' : //推荐的产品
                    $datas[$param['key']]=$this->getTuiGoodsData($param);
                    break;
                case 'tuan'://抢购
                    $datas[$param['key']]=$this->getSnapUpData($param['num']);
                    break;
                case 'oncecard':
                    $datas[$param['key']]=$this->getOncecardData($param['num']);
                    break;
                case 'look':
                    $datas[$param['key']] = $this->getLookData($param['num']);
                    break;
                case 'project':
                    $datas[$param['key']]=$this->getProjectData($param['num']);
                    break;
            }
        }
        return $datas;
    }
    protected function getLookData($limit){
        $where=[
            ['shop_id','=',$this->shopId],
            ['is_delete','=',0],
        ];
        $datas=LookModel::where($where)
            ->order(['update_time'=>'desc'])
            ->field('look_id,photos,info,see_num,likes_num,add_time')
            ->limit($limit)
            ->select()
            ->toArray();
        return $datas;
    }
    protected function getCouponData($limit)
    {
        $where=[
            ['shop_id','=',$this->shopId],
            ['status','=',1],
            ['is_delete','=',0],
            ['quota','>',0],
            ['start_time','<=',$this->today],
            ['end_time','>=',$this->today],
            ['is_integral_exchange','=',0]
        ];
        $return = [];
        $return['hasIds'] = [];
        //如果memberId 不为空的情况下
        if($this->memberId){
            $member = MemberModel::find($this->memberId);
            $where[]= $member->level === null ? ['type_id','=',1] : ['type_id','IN',[1,3]];

            $hasIds = MemberCouponModel::where([
                    ['member_id','=', $this->member_id],
                    ['shop_id','=', $this->shop_id],
                    ['type_id','=',1]
                ])->column('coupon_id') ?? [];
            $return['hasIds'] = $hasIds;
        }else{ //非登录情况只显示普通券
            $where[]=['type_id','=',1];
        }

        $coupons = CouponModel::where($where)
            ->order(['update_time'=>'desc','add_time'=>'desc'])
            ->limit($limit)
            ->field('coupon_id,name,valid_bg_time,valid_end_time,coupon_money,money_limit')
            ->select()->toArray();
        $return['coupons']=$coupons;
       return $return;
    }

    protected function getFlashSaleData($limit)
    {
        $where=[
            ['shop_id','=',$this->shopId],
            ['status','=',1],
            ['is_delete','=',0],
            ['start_time','<=',$this->today],
            ['end_time','>=',$this->today],
        ];
        //获取可用的商品id集合
        $goods_where=[
            ['shop_id','=',$this->shopId],
            ['status','=',1],
            ['is_delete','=',0],
            ['is_open_yu','=',0],
        ];
        $goods_ids=GoodsModel::where($goods_where)->column('goods_id') ?? [];
        $where[]=['goods_id','in',$goods_ids];

        $order=['is_tui'=>'desc','fictitious_take_count'=>'desc','flash_sale_price'=>'asc'];
        $datas=FlashSaleGoodsModel::with(['goods'=>function($query){
            $query->field('goods_id,name,original_price,cover_img');
        }])->where($where)
            ->order($order)
            ->field('flash_sale_goods_id,goods_id,flash_sale_price,fictitious_take_count,quota,end_time,take_count')
            ->limit($limit)
            ->select()
            ->append(['flash_sale_djs','flash_sale_progress'])
            ->toArray();
        return $datas;
    }

    protected function getPreSaleData($limit)
    {
        $where=[
            ['shop_id','=',$this->shopId],
            ['status','=',1],
            ['is_delete','=',0],
            ['is_open_yu','=',1],
        ];

        $order=['sort'=>'desc','fictitious_take_count'=>'desc','vip_price'=>'desc'];
        $datas=GoodsModel::where($where)
            ->order($order)
            ->field('goods_id,cover_img,name,original_price,quota,fictitious_take_count,is_open_yu,yu_days,yu_price')
            ->limit($limit)
            ->select()
            ->append(['pre_sale_time'])
            ->toArray();
        return $datas;
    }

    protected function getPinTuanData($limit)
    {
        $where=[
            ['shop_id','=',$this->shopId],
            ['status','=',1],
            ['is_delete','=',0],
            ['start_time','<=',$this->today],
            ['end_time','>=',$this->today],
        ];
        //获取可用的商品id集合
        $goods_where=[
            ['shop_id','=',$this->shopId],
            ['status','=',1],
            ['is_delete','=',0],
            ['is_open_yu','=',0],
        ];
        $goods_ids=GoodsModel::where($goods_where)->column('goods_id') ?? [];
        $where[]=['goods_id','in',$goods_ids];

        $order=['is_tui'=>'desc','fictitious_take_count'=>'desc','pin_tuan_price'=>'asc'];
        $datas=PinTuanGoodsModel::with(['goods'=>function($query){
            $query->field('goods_id,name,original_price,cover_img');
        }])->where($where)
            ->order($order)
            ->field('pin_tuan_goods_id,group_num,goods_id,pin_tuan_price,fictitious_take_count,quota')
            ->limit($limit)
            ->select()
            ->toArray();
        return $datas;
    }

    protected function getStoreData($limit)
    {
        $where=[
            ['shop_id','=',$this->shopId],
            ['status','=',1],
            ['is_delete','=',0],
        ];
        //获取可用的商品id集合
        $goods_where=[
            ['shop_id','=',$this->shopId],
            ['status','=',1],
            ['is_delete','=',0],
            ['is_open_yu','=',0],
        ];
        $goods_ids=GoodsModel::where($goods_where)->column('goods_id') ?? [];
        $where[]=['goods_id','in',$goods_ids];

        $order=['is_tui'=>'desc','fictitious_take_count'=>'desc','total_price'=>'asc'];
        $datas=StoreGoodsModel::with(['goods'=>function($query){
            $query->field('goods_id,name,cover_img');
        }])->where($where)
            ->order($order)
            ->field('store_goods_id,num,goods_id,total_price,worth,fictitious_take_count,gift_giving')
            ->limit($limit)
            ->select()
            ->append(['gift_total_price'])
            ->toArray();
        return $datas;
    }

    protected function getTuiGoodsData($param)
    {
        $limit = $param['num'] ?? 8;
        $where=[
            ['shop_id','=',$this->shopId],
            ['status','=',1],
            ['is_delete','=',0],
            ['is_open_yu','=',0],
        ];
        if(isset($param['category'])){ //暂时只支持二级分类
            $category_id =  (int)$param['category'];
            if(!empty($category_id)){
                $where[] = ['category_id','=',$category_id];
            }
        }

        $order=['sort'=>'desc','fictitious_take_count'=>'desc','vip_price'=>'desc'];

        $datas=GoodsModel::where($where)
            ->order($order)
            ->field('goods_id,cover_img,name,original_price,vip_price,quota,take_count,fictitious_take_count')
            ->limit($limit)
            ->select()
            ->toArray();
        return $datas;
    }

    protected function getOncecardData($limit){
        $where=[
            ['shop_id','=',$this->shopId],
            ['is_delete','=',0],
            ['status','=',1]
        ];
        $order=['sort'=>'desc','add_time'=>'desc'];
        $append=['add_time_format','is_flash_sale_expire','flash_sale_djs','save_money'];
        $cards =  OnceCardModel::where($where)->order($order)->limit($limit)->page(1)->select()->append($append)->hidden(['snap_up_details','purchase_information','rotation'])->toArray();
        return  ['oncecard'=>$cards];

    }

    protected function getSnapUpData($limit){
        $where=[
            ['shop_id','=',$this->shopId],
            ['is_delete','=',0],
            ['status','=',1]
        ];

        $project_where=[
            ['shop_id','=',$this->shopId],
            ['is_delete','=',0],
        ];
        $project_ids= ProjectModel::where($project_where)->column('project_id') ?? [];
        $where[]=['project_id','in',$project_ids];

        $order=['sort'=>'desc','add_time'=>'desc'];
        $append=['add_time_format','is_flash_sale_expire','flash_sale_djs'];
        $tuans =  SnapUpModel::with(['project'])->where($where)->order($order)->limit($limit)->page(1)->select()->append($append)->hidden(['snap_up_details','purchase_information','rotation'])->toArray();
        return  ['tuans'=>$tuans];
    }

    protected function getProjectData($limit)
    {
        $where=[
            ['shop_id','=',$this->shopId],
            ['is_delete','=',0],
        ];
        $datas = ProjectModel::where($where)
            ->field('project_id,abbreviation,market_price,cover_img')->limit($limit)->page(1)->select()->toArray();
        return  $datas;
    }

}
